#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : login_worker.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/4/24 13:56
"""
import json
import traceback
from PyQt5.QtCore import QThread, pyqtSignal
from utils.tools import request
from config import Config


class LoginWorkerQtThread(QThread):
    """登录"""
    result = pyqtSignal(dict)
    failed = pyqtSignal(Exception)


    def __init__(self, username, password, parent=None):
        super(LoginWorkerQtThread, self).__init__()
        self.username = username
        self.password = password


    def run(self):
        try:
            data = {
                "username": self.username,
                "password": self.password
            }
            headers = {'Content-Type': 'application/json'}
            response = request(f'{Config.Host}/api/login/', method='POST', data=json.dumps(data), headers=headers)
            self.result.emit(response.json())
        except Exception as e:
            traceback.print_exc()
            self.failed.emit(e)
